package cn.pconline.autoclub.entity;

import cn.pconline.autoclub.util.EncodeUtils;

import com.alibaba.fastjson.JSONObject;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.gelivable.dao.Column;
import org.gelivable.dao.Entity;
import org.gelivable.dao.Id;
import org.gelivable.dao.Transient;

/**
 * @author 作者 guoqiang
 * @version 2014-1-21 车友活动
 */
@Entity(tableName = "ac_activity")
public class Activity implements EntityWithClubProp{

    public static final int STATUS_NO = 0;
    public static final int STATUS_YES = 1;
    public static final int TYPE_DINNER = 1; //聚餐
    public static final int TYPE_DRIVING = 2; //自驾游
    public static final int TYPE_OTHER = 3; //其他
    
    /**标题字符**/
    public static final int TITLE_SIZE = 60;
    /**
     * 活动ID
     */
    @Id
    @Column(name = "activity_id")
    private long activityId;
    /**
     * 车友会ID
     */
    @Column(name = "club_id")
    private long clubId;
    /**
     * 标题
     */
    @Column(name = "title")
    private String title;
    /**
     * 内容
     */
    @Column(name = "context")
    private String context;
    /**
     * 内容是否超出255 0--<=255 1-->255
     */
    @Column(name = "long_content")
    private int longContext;
    /**
     * 活动开始时间
     */
    @Column(name = "begin_date")
    private Date beginDate;
    /**
     * 活动结束时间
     */
    @Column(name = "end_date")
    private Date endDate;
    /**
     * 报名截止时间
     */
    @Column(name = "signup_date")
    private Date signupDate;
    /**
     * 是否线上活动 1--线上 0--线下
     */
    @Column(name = "online_status")
    private int onlineStatus;
    /**
     * 省
     */
    @Column(name = "province_id")
    private long provinceId;
    /**
     * 省name
     */
    @Column(name = "province_name")
    private String provinceName;
    /**
     * 城市
     */
    @Column(name = "city_id")
    private long cityId;
    /**
     * 城市name
     */
    @Column(name = "city_name")
    private String cityName;
    /**
     * 活动经费
     */
    @Column(name = "money")
    private int money;
    /**
     * 标题图链接url 300x200
     */
    @Column(name = "pic_url")
    private String picUrl;
    /**
     * 参加人数
     */
    @Column(name = "join_num")
    private int joinNum;
    /**
     * 活动发起人
     */
    @Column(name = "create_by")
    private long createBy;
    /**
     * 创建时间
     */
    @Column(name = "create_at")
    private Date createAt;
    /**
     * 活动状态
     */
    @Column(name = "status")
    private int status;
    /**
     * 活动报名上限
     */
    @Column(name = "max_join")
    private int maxJoin;
    /**
     * 活动类型
     */
    @Column(name = "type")
    private int type;
    
    @Transient
    public String url160x120;
    @Transient
    public String url300x200;
    /**
     * 活动状态显示code
     */
    @Transient
    public String userActivityStatus;
    /**
     * 活动状态显示msg
     */
    @Transient
    public String userActivityStatusMsg;
    /**
     * 完整内容
     */
    @Transient(cache = false)
    private String fullContent;
    
    /**
     * 活动发布者
     */
    @Transient
    private User creator;
    
    /*
     * 活动申请人员列表
     */
    @Transient
    private List<ActivityMember> activityMemberList;

    /**
     * 车友会
     */
    @Transient
    private Club club;
    
    /**
     * 成功报名人数
     */
    @Transient
    private int applyPassCount;
    
    /**
     * 成功报名人数
     */
    @Transient
    private String activityPlace;
    
	public String getActivityPlace() {
		String activityPlace = "";
		if(this.type == Activity.TYPE_OTHER) {
        	activityPlace = "线上";
        } else if(StringUtils.isNotBlank(this.provinceName)) {
        	activityPlace = this.provinceName;
        	if(StringUtils.isNotBlank(this.cityName) && !activityPlace.equals(this.cityName)) {
            	activityPlace += " - " + this.cityName;
            }
        } 
		return activityPlace;
	}
	
	public void setActivityPlace(String activityPlace) {
		this.activityPlace = activityPlace;
	}

	public int getApplyPassCount() {
		return applyPassCount;
	}

	public void setApplyPassCount(int applyPassCount) {
		this.applyPassCount = applyPassCount;
	}

	public List<ActivityMember> getActivityMemberList() {
		return activityMemberList;
	}

	public void setActivityMemberList(List<ActivityMember> activityMemberList) {
		this.activityMemberList = activityMemberList;
	}

	public User getCreator() {
		return creator;
	}

	public void setCreator(User creator) {
		this.creator = creator;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	// 160x120图片url拼接
    public String getUrl160x120() {
        if (StringUtils.isBlank(picUrl)) {
            return "";
        }
        return picUrl.replace("300x200", "160x120");
    }

    public String getUrl120x120() {
        if (StringUtils.isBlank(picUrl)) {
            return "";
        }
        return picUrl.replace("300x200", "120x120");
    }
    
    public String getUrl160x107() {
        if (StringUtils.isBlank(picUrl)) {
            return "";
        }
        return picUrl.replace("300x200", "160x107");
    }
    
    public String getUrl600x400() {
        if (StringUtils.isBlank(picUrl)) {
            return "";
        }
        return picUrl.replace("300x200", "600x400");
    }

    public long getActivityId() {
        return activityId;
    }

    public void setActivityId(long activityId) {
        this.activityId = activityId;
    }

    public long getClubId() {
        return clubId;
    }

    public void setClubId(long clubId) {
        this.clubId = clubId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public int getLongContext() {
        return longContext;
    }

    public void setLongContext(int longContext) {
        this.longContext = longContext;
    }

    public Date getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Date getSignupDate() {
        return signupDate;
    }

    public void setSignupDate(Date signupDate) {
        this.signupDate = signupDate;
    }

    public int getOnlineStatus() {
        return onlineStatus;
    }

    public void setOnlineStatus(int onlineStatus) {
        this.onlineStatus = onlineStatus;
    }

    public long getProvinceId() {
        return provinceId;
    }

    public void setProvinceId(long provinceId) {
        this.provinceId = provinceId;
    }

    public String getProvinceName() {
        return provinceName;
    }

    public void setProvinceName(String provinceName) {
        this.provinceName = provinceName;
    }

    public long getCityId() {
        return cityId;
    }

    public void setCityId(long cityId) {
        this.cityId = cityId;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getPicUrl() {
        return picUrl;
    }

    public void setPicUrl(String picUrl) {
        this.picUrl = picUrl;
    }

    public int getJoinNum() {
        return joinNum;
    }

    public void setJoinNum(int joinNum) {
        this.joinNum = joinNum;
    }

    public long getCreateBy() {
        return createBy;
    }

    public void setCreateBy(long createBy) {
        this.createBy = createBy;
    }

    public Date getCreateAt() {
        return createAt;
    }

    public void setCreateAt(Date createAt) {
        this.createAt = createAt;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getFullContent_660() {
        Pattern pattern = Pattern.compile("\\[img=\\d+,\\d+\\]");
        Matcher matcher = pattern.matcher(fullContent);
        StringBuffer sbr = new StringBuffer();
        while (matcher.find()) {
            String img = matcher.group();
            try {
                int eqIndex = img.indexOf("=");
                int dotIndex = img.indexOf(",");
                int w = Integer.parseInt(img.substring(eqIndex + 1, dotIndex));
                int endIndex = img.indexOf("]");
                int h = Integer.parseInt(img.substring(dotIndex + 1, endIndex));
                if (w > 660) {
                    h = 10000 * h / w * 660 / 10000;
                    w = 660;
                    img = "[img=" + w + "," + h + "]";
                }
            } catch (java.lang.NumberFormatException e) {
                //可能是脏数据，忽略
            }
            matcher.appendReplacement(sbr, img);
        }
        matcher.appendTail(sbr);
        return sbr.toString();
    }

    public String getFullContent() {
        return fullContent;
    }

    public void setFullContent(String fullContent) {
        this.fullContent = fullContent;
    }

    public int getMaxJoin() {
        return maxJoin;
    }

    public void setMaxJoin(int maxJoin) {
        this.maxJoin = maxJoin;
    }

    public String getUserActivityStatus() {
        return userActivityStatus;
    }

    public void setUserActivityStatus(String userActivityStatus) {
        this.userActivityStatus = userActivityStatus;
    }

    public String getUserActivityStatusMsg() {
        return userActivityStatusMsg;
    }

    public void setUserActivityStatusMsg(String userActivityStatusMsg) {
        this.userActivityStatusMsg = userActivityStatusMsg;
    }

    public JSONObject toJson() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("activityId", getActivityId());
        jsonObject.put("title", getTitle());
        return jsonObject;
    }

    public JSONObject toJsonEncodeHTML() {
        JSONObject jsonObject = toJson();
        jsonObject.put("title", EncodeUtils.encodeForHTMLFilterNull(getTitle()));
        return jsonObject;
    }

    public Club getClub() {
       return club;
    }

    public void setClub(Club club) {
        this.club = club;
    }
}
